﻿using Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class UserManagement
    {

        #region//查询指定用户
        /// <summary>
        /// 将输入的账号和密码与数据库对比，并返回用户实体类对象
        /// </summary>
        /// <param name="admin"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static UserLogin GetUser(string admin, string pwd)
        {
            String sql = "select * from [User] where admin='{0}' and pwd = '{1}'";
            sql = string.Format(sql, admin, pwd);
            UserLogin userLogin = new UserLogin();
            try
            {
                SqlDataReader objreader = SQLHelp.GetReader(sql);
                if (objreader.Read())
                {
                    userLogin.Admin = admin;
                    userLogin.Pwd = pwd;
                    userLogin.UserID = Convert.ToInt32(objreader["userID"].ToString());
                    userLogin.name = objreader["name"].ToString();
                }
                else
                {
                    userLogin.Admin = null;
                }
                objreader.Close();
            }
            catch (Exception ex)
            {
                //将错误打印出来
                throw new Exception("数据访问发生异常！" + ex.Message);
            }
            return userLogin;
        }
        #endregion
        #region//查询指定用户
        /// <summary>
        /// 将输入的账号和密码与数据库对比，并返回用户实体类对象
        /// </summary>
        /// <param name="admin"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static UserLogin GetUser(string admin)
        {
            String sql = "select userID from [User] where admin='{0}'";
            sql = string.Format(sql, admin);
            UserLogin userLogin = new UserLogin();
            try
            {
                SqlDataReader objreader = SQLHelp.GetReader(sql);
                if (objreader.Read())
                {
                    userLogin.Admin = admin;
                    userLogin.UserID = Convert.ToInt32(objreader["userID"].ToString());
                }
                else
                {
                    userLogin.Admin = null;
                }
                objreader.Close();
            }
            catch (Exception ex)
            {
                //将错误打印出来
                throw new Exception("数据访问发生异常！" + ex.Message);
            }
            return userLogin;
        }
        #endregion


        #region//添加新用户
        public static int AddUser(string userID, string admin, string pwd,int serialNumber,string name)
        {
            string sql = "insert into [User](userID,admin,pwd,serialNumber,name) values('{0}','{1}','{2}','{3}','{4}')";
            sql = string.Format(sql, userID, admin, pwd, serialNumber,name);
            int rnum = SQLHelp.AddData(sql);
            return rnum;
        }


        #endregion
        #region//修改用户密码
        public static int UpdateUser(string admin, string pwd,string userID, int serialNumber, string name)
        {
            string sql = "update [User] set pwd = '{0}',userID = '{1}',serialNumber = '{2}',name = '{3}' where admin = '{4}'";
            sql = string.Format(sql, pwd, userID, serialNumber, name,admin);
            int rnum = SQLHelp.AddData(sql);
            return rnum;
        }


        #endregion
        #region//删除用户
        public static int DeleteUser(string admin)
        {
            string sql = "delete from [User] where admin = '{0}'";
            sql = string.Format(sql, admin);
            int rnum = SQLHelp.AddData(sql);
            return rnum;
        }

        #endregion
    }
}
